# !/bin/sh

if [ -z "$VOLUME_BACKUP" -o -z "$BASENAME" ];
then
	echo "Error: env var not set!!!" 1>&2
	exit 1
fi

srcdir=.
destdir=$VOLUME_BACKUP/$BASENAME

tmpfile=/tmp/filelist.log

if [ "`pwd`" = "$destdir" ];
then
	echo "Error: source dir is the same as destnation dir!!!" 1>&2
	exit 2
fi

find $srcdir -name "*" -type f -print0 | xargs -0 printf "%s\n" > $tmpfile
cat $tmpfile | while read bfile
do
	if [ -z "$bfile" ];
	then
		continue
	fi

	if [ ! -e $destdir/"$bfile" -o "$bfile" -nt $destdir/"$bfile" ];
	#if [ true ];
	then
		dir=`dirname "$bfile"`
		if [ ! -d $destdir/"$dir" ];
		then
			mkdir -p $destdir/"$dir"
		fi
		echo "Copy $bfile"
		cp "$bfile" $destdir/"$bfile"
	else
		echo "File existed: $bfile"
	fi
done
sync
rm $tmpfile

